package com.RedRiver.hospitalsystem.personal_service.common.util;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Date;
import java.util.Map;

@Component
public class TokenTemplate {
    @Value("${jjwt.sect}")
    private String sect;
    @Value("${jjwt.time}")
    private Integer time;

    public String createToken(Map<String ,Object> body){
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder().setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis()+time*60*1000))
                .signWith(key)
                .compact();
    }
    //解析token
    public Claims parseToken(String token) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }
}
